package cz.incad.psh.pshassign.server;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Writer;
import java.util.ArrayList;
import org.apache.commons.io.FileUtils;

/**
 *
 * @author Alberto Hernandez
 */
public class StopWords {

    public static ArrayList<String> loadAll() throws Exception {
        ArrayList<String> stopWords = new ArrayList<String>();
        stopWords.addAll(loadCommon());
        stopWords.addAll(load());
        return stopWords;
    }
    
    public static ArrayList<String> loadFile(String filename) throws Exception {
        String path = System.getProperty("user.home") + "/.psh/" + filename;
        
        File f = new File(path);
        if(!f.exists()){
            f = FileUtils.toFile(Options.class.getResource("/cz/incad/psh/pshassign/"+filename));
        }
                
        ArrayList<String> stopWords = new ArrayList<String>();
            FileInputStream in = new FileInputStream(f);
            //DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
            while ((strLine = br.readLine()) != null) {
                stopWords.add(strLine);
            }
            in.close();
        return stopWords;
    } 
    
    public static String removeStopWords(String old) throws Exception {
        ArrayList<String> stopWords = loadAll();
        String s = old;
        for (String word : stopWords) {
            s = s.replaceAll("\\b"+word+"\\b", "");
        }
        return s;
    }

    public static ArrayList<String> load() throws Exception {
        return loadFile("stop.txt");
    }

    public static ArrayList<String> loadCommon() throws Exception {
        return loadFile("stop_common.txt");
    }

    public static void add(String word) throws Exception {
        Writer output;
        String path = System.getProperty("user.home") + "/.psh/stop.txt";
        
        File f = new File(path);
        if(!f.exists()){
            f = FileUtils.toFile(Options.class.getResource("/cz/incad/psh/pshassign/stop.txt"));
        }
        
        output = new BufferedWriter(new FileWriter(f, true));
        output.append("\n"+word);
        output.close();
    }

    public static void remove(String word) throws Exception {
        ArrayList<String> sw = load();
        //File tempFile = File.createTempFile("stop",".tmp");
        String path = System.getProperty("user.home") + "/.psh/stop.txt";
        File f = new File(path);
        if(!f.exists()){
            f = FileUtils.toFile(Options.class.getResource("/cz/incad/psh/pshassign/stop.txt"));
        }
        BufferedWriter writer = new BufferedWriter(new FileWriter(f));
        
        sw.remove(word);

       for(String s:sw){
            // trim newline when comparing with lineToRemove
            String trimmedLine = s.trim();
            if(!trimmedLine.equals(word)) {
                writer.write(s + "\n");
            }
        }
       writer.flush();
       writer.close();
    }

}
